package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.*;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.UserDO;
import com.wisewin.circle.entity.db.UserFollowDO;
import com.wisewin.circle.entity.db.UserVisitDO;
import com.wisewin.circle.entity.dto.ActivitySignUserDTO;
import com.wisewin.circle.util.*;
import com.wisewin.circle.util.tencentyun.TencentCloudImUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserService {
	final static Logger log = LoggerFactory.getLogger(UserService.class);

	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserVisitDAO userVisitDAO;
	@Autowired
	private UserFollowDAO userFollowDAO;

	public void insertPhone(UserDO userDO){
		userDAO.insertPhone(userDO);
	}

	// 根据手机号码查询用户
	public LoginUser queryUserInfoByPhone(String phone){
		return userDAO.queryUserInfoByPhone(phone);
	}

	// 修改个人主页图片接口
	public ResultDTO updateUserHomeUrl(UserDO userDo){
		if(StringUtils.isEmpty(userDo.getHomeUrl())){
			return ResultDTOBuilder.failure("1", "请上传图片", null);
		}
		userDAO.updateUserHomeUrl(userDo);
		return ResultDTOBuilder.success(userDo);
	}

	// 查询用户访客记录
	public ResultDTO queryUserVisitList(QueryParamBO queryParamBO, PageUtil pageUtil){
		Map<String, Object> map = new HashMap<>();
		map.put("userId", queryParamBO.getUserId());
		map.put("pageOffset", pageUtil.getPageNo());
		map.put("pageSize", pageUtil.getPageSize());
		PageObjectUtil page = new PageObjectUtil();
		PageInfo pageData = page.savePageObject(userVisitDAO.queryUserVisitCount(map), userVisitDAO.queryUserVisitList(map), pageUtil);
		if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
			return ResultDTOBuilder.success(pageData);
		}
		// 处理数据
		for(ActivitySignUserDTO activitySignUserDTO : (List<ActivitySignUserDTO>)pageData.getRecords()){
			// 是否关注
			Integer ifFollow = activitySignUserDTO.getId() == queryParamBO.getUserId() ? 1 : activitySignUserDTO.getIfFollow();
			activitySignUserDTO.setIfFollow(ifFollow);
			activitySignUserDTO.setIfCanCancle(activitySignUserDTO.getId() == queryParamBO.getUserId() ? 0 : 1);
		}
		return ResultDTOBuilder.success(pageData);
	}

	// 添加访问用户
	public ResultDTO addUserVisit(UserVisitDO userVisitDO){
		if(userVisitDO.getVisitUserId() == null){
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		// 本身不添加记录
		if(userVisitDO.getUserId() == userVisitDO.getVisitUserId()){
			return ResultDTOBuilder.success(userVisitDO);
		}
		UserDO userDO = userDAO.queryUserById(userVisitDO.getVisitUserId());
		if(userDO == null){
			return ResultDTOBuilder.failure("1", "当前用户不存在", null);
		}
		// 添加记录
		userVisitDAO.insert(userVisitDO);
		// 修改用户访问数量
		userDAO.updateVisitNum(userVisitDO.getVisitUserId());
		return ResultDTOBuilder.success(userVisitDO);
	}

	// 启用、禁用用户
	public ResultDTO updateUserStatus(QueryParamBO queryParamBO){
		if(queryParamBO.getUserId() == null || queryParamBO.getType() == null){
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		if(queryParamBO.getType() != 0 && queryParamBO.getType() != 1){
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		// 修改用户状态
		userDAO.updateUserStatus(queryParamBO.getUserId(), queryParamBO.getType());
		return ResultDTOBuilder.success(queryParamBO.getUserId());
	}

	// 删除用户
	public ResultDTO deleteUser(Integer userId){
		if(userId == null){
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		UserDO userDO = userDAO.queryUserById(userId);
		if(userDO == null){
			return ResultDTOBuilder.failure("1", "当前用户不存在", null);
		}
		userDAO.updateUserIsDel(userId);
		return ResultDTOBuilder.success(userId);
	}

	// 创建用户
	public ResultDTO addUser(UserDO userDO){
		if(StringUtils.isEmpty(userDO.getHeadUrl())){
			return ResultDTOBuilder.failure("1", "请上传用户头像", null);
		}
		if(StringUtils.isEmpty(userDO.getUserName())){
			return ResultDTOBuilder.failure("1", "请填写用户名", null);
		}
		if(StringUtils.isEmpty(userDO.getPhone())){
			return ResultDTOBuilder.failure("1", "请填写手机号码", null);
		}
		// 查询手机号码是否存在
		UserDO userOld = userDAO.queryUserByPhone(userDO.getPhone());
		if(userOld != null){
			return ResultDTOBuilder.failure("1", "手机号码已存在", null);
		}
		// 处理密码
		userDO.setPasscode(MD5Util.digest(userDO.getPhone()));
		// 添加用户
		userDAO.insert(userDO);
		return ResultDTOBuilder.success(null);
	}

	// 查询用户列表
	public ResultDTO queryUserList(QueryParamBO queryParamBO, PageUtil pageUtil){
		Map<String, Object> map = new HashMap<>();
		map.put("userName", queryParamBO.getUserName());
		map.put("phone", queryParamBO.getPhone());
		map.put("beginTime", queryParamBO.getBeginTime());
		map.put("endTime", queryParamBO.getEndTime());
		map.put("pageOffset", pageUtil.getPageNo());
		map.put("pageSize", pageUtil.getPageSize());
		PageObjectUtil page = new PageObjectUtil();
		PageInfo pageData = page.savePageObject(userDAO.queryUserCount(map), userDAO.queryUserList(map), pageUtil);
		return ResultDTOBuilder.success(pageData);
	}

	// 修改用户基本信息
	public ResultDTO updateUserInfo(UserDO userDO){
		if(userDO.getId() == null){
			return ResultDTOBuilder.failure("1", "参数有误", null) ;
		}
		if(StringUtils.isEmpty(userDO.getHeadUrl())){
			return ResultDTOBuilder.failure("1", "请上传用户头像", null) ;
		}
		if(StringUtils.isEmpty(userDO.getUserName())){
			return ResultDTOBuilder.failure("1", "请填写用户名", null) ;
		}
		if(StringUtils.isEmpty(userDO.getIntroduce())){
			return ResultDTOBuilder.failure("1", "请填写简介信息", null) ;
		}
		UserDO userOld = userDAO.queryUserById(userDO.getId());
		if(userOld == null){
			return ResultDTOBuilder.failure("1", "用户不存在", null) ;
		}
		String homeUrl = StringUtils.isEmpty(userDO.getHomeUrl()) ? null : userDO.getHomeUrl();
		userDO.setHomeUrl(homeUrl);
		// 修改用户信息
		userDAO.updateUserInfo(userDO);
		// 设置资料
		TencentCloudImUtil tencentCloudImUtil = new TencentCloudImUtil();
		tencentCloudImUtil.portraitSet(userDO.getId().toString(), userDO.getUserName(), userDO.getHeadUrl());
		return ResultDTOBuilder.success(userDO);
	}

	// 查询用户信息
	public ResultDTO queryUserInfo(Integer userId, Integer loginUserId, Integer visitUserId){
		UserDO userDO = userDAO.queryUserById(userId);
		if(userDO == null){
			return ResultDTOBuilder.failure("1", "用户不存在", null) ;
		}
		// 是否可以关注
		Integer ifFollow = 1;
		Integer ifCanCancle = 0;
		if(userId != loginUserId){
			UserFollowDO userFollowDO = userFollowDAO.queryUserFollow(loginUserId, userId);
			ifFollow = userFollowDO == null ? 0 : 1;
			ifCanCancle = 1;
		}
		userDO.setIfFollow(ifFollow);
		userDO.setIfCanCancle(ifCanCancle);
		// 添加访问记录
		if(visitUserId != null){
			UserVisitDO userVisitDO = new UserVisitDO();
			userVisitDO.setUserId(loginUserId);
			userVisitDO.setVisitUserId(visitUserId);
			addUserVisit(userVisitDO);
		}
		// 处理访问人数
		Integer visitNum = userVisitDAO.queryUserVisitNum(userId);
		userDO.setVisitNum(visitNum);
		return ResultDTOBuilder.success(userDO);
	}

	public ResultDTO queryUserById(Integer userId){
		if(userId == null){
			return ResultDTOBuilder.failure("1", "参数有误", null) ;
		}
		UserDO userDO = userDAO.queryUserById(userId);
		return ResultDTOBuilder.success(userDO);
	}

	public UserDO queryUserByPhonePasscode(UserDO userDO){
		return userDAO.queryUserByPhonePasscode(userDO.getPhone(), userDO.getPasscode());
	}
}
